﻿using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using FriendlyURL.Web.Domain.Impl;
using Ninject;
using Ninject.Web.Common;

namespace FriendlyURL.Web.UI
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : NinjectHttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");       

            routes.MapRoute(
             "Activate",
             "Account/Activate/{username}/{key}",
             new
             {
                 controller = "Account",
                 action = "Activate",
                 username = UrlParameter.Optional,
                 key = UrlParameter.Optional
             });

            routes.MapRoute(
                "Alias",
                "Alias/{action}/{aliasName}",
                new
                {
                    controller = "Alias",
                    action = UrlParameter.Optional,
                    aliasName = UrlParameter.Optional
                });


            routes.MapRoute(
                "Redirect",
                "{alias}",
                new
                {
                    controller = "Alias",
                    action = "RedirectAlias"
                });  

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new 
                { 
                    controller = "Home", 
                    action = "Index", 
                    id = UrlParameter.Optional 
                });
        }

        protected override void OnApplicationStarted()
        {
            AreaRegistration.RegisterAllAreas();
            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);

            // Tell the MVC Framework to use our implementation of metadataprovider.
            ModelMetadataProviders.Current = new MetadataProvider();

            DIFactory.GetNinjectFactory().Inject(Membership.Provider);
            DIFactory.GetNinjectFactory().Inject(Roles.Provider);
        }

        /// <summary>
        /// Establish a reference to our DIFactory object
        /// <remarks>
        /// This application currently uses Ninject for dependency injection.
        /// </remarks>
        /// </summary>
        /// <returns></returns>
        protected override IKernel CreateKernel()
        {
            return DIFactory.GetNinjectFactory();
        }
    }
}